package com.gticket.customerbackend.handler;

import com.gticket.customerbackend.pojo.Payment;
import com.gticket.customerbackend.pojo.ResponseDate;
import com.gticket.customerbackend.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Data
@RequestMapping("/payment")
@Api(tags = "支付方式")
public class PaymentHandler {
    @Autowired
    private PaymentService paymentService;

    @ApiOperation(value = "展示所有支付方式")
//    @Cacheable(value = "listPayment",keyGenerator = "myKeyGenerator")
    @GetMapping
    public ResponseDate listPayment(){
        ResponseDate responseDate=new ResponseDate();
        Payment payment =new Payment();
        try {
            List<Payment> payments=paymentService.listPayment(payment);
            responseDate.setCode(300);
            responseDate.setMsg("查询成功");
            responseDate.setData(payments);
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;
    }

    @ApiOperation(value = "通过cinema_id查询payment")
//    @Cacheable(value = "payment",keyGenerator = "myKeyGenerator")
    @GetMapping("/id")
    public ResponseDate getPaymentById(Payment payment){
        ResponseDate responseDate=new ResponseDate();
        try {
            Payment payment2=paymentService.getPaymentById(payment);
            responseDate.setCode(200);
            responseDate.setMsg("查询成功");
            responseDate.setData(payment2);
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;
    }

    @ApiOperation(value = "修改payment")
    @PutMapping
    public ResponseDate updatePayment(Payment payment){
        ResponseDate responseDate=new ResponseDate();
        try {
           Payment payment1= paymentService.getPaymentById(payment);//通过影院id查询是否支付宝信息存在
            //如果不存在就重新添加
            System.out.println("++++++++++++++++"+payment1);
           if(payment1 == null){
               paymentService.insertPayment(payment);
           }else {
               //存在就修改
               paymentService.updatePayment(payment);
           }
            responseDate.setCode(200);
            responseDate.setMsg("修改成功");
            responseDate.setData(payment);
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;
    }

    @ApiOperation(value = "添加payment")
    @PostMapping
    public void insertPayment(Payment payment){
        ResponseDate responseDate=new ResponseDate();
//        try {
            paymentService.insertPayment(payment);
//            responseDate.setCode(200);
//            responseDate.setMsg("修改成功");
//            responseDate.setData(payment);
//        }catch (Exception e){
//            responseDate.setCode(500);
//            responseDate.setMsg("接口异常");
//        }
//        return responseDate;
    }



}
